﻿using Servo_Tool.constant;
using Servo_Tool.domain;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Servo_Tool.utils
{
    public class EquationUtils
    {
        /// <summary>
        /// 根据条件内容生成事件触发链
        /// </summary>
        /// <param name="cons"></param>
        /// <returns></returns>
        public static List<EquationLinks_EVENT> Cons2EquationLinks(string cons)
        {
            //0.内容判断是否为空
            if (string.IsNullOrEmpty(cons))
            {
                return null;
            }
            //1.对内容进行特殊处理(替换特殊字符并转换为大写)
            string content = cons.Replace("[", "").Replace("]", "").Trim();
            //string content = cons.ToUpper().Trim();
            //2.内容中是否包含可拆分字符
            if (!content.Contains(SymbolConstants.SYMBOL_EQUAL) && !content.Contains(SymbolConstants.SYMBOL_NO_EQUAL))
            {
                return null;
            }

            //3.创建一个事件触发链的List
            List<EquationLinks_EVENT> eventLinks = new List<EquationLinks_EVENT>();
            //4.先以分号;拆分内容(每一部分都是一个整体),单独一个判定层级(同时成立方能触发)
            string[] semicolons = Regex.Split(content, SymbolConstants.SYMBOL_SEMICOLON, RegexOptions.IgnoreCase);
            foreach (string part in semicolons)
            {
                //4.1.以UUID确定条件层级
                string rankNo = Guid.NewGuid().ToString("N");
                //4.2.构建事件链
                EquationLinks_EVENT eventLink = new EquationLinks_EVENT
                {
                    RANK_NO = rankNo
                };
                //4.3将内容转换为List<Equation_EVENT>
                List<Equation_EVENT> equations = Cons2Equations(part);

                eventLink.EQUATIONS = equations;
                eventLinks.Add(eventLink);
            }

            return eventLinks;
        }

        /// <summary>
        /// 将字符串转换为条件(多个条件)
        /// </summary>
        /// <param name="cons"></param>
        /// <returns></returns>
        public static List<Equation_EVENT> Cons2Equations(string cons)
        {
            //0.内容判断是否为空
            if (string.IsNullOrEmpty(cons))
            {
                return null;
            }
            List<Equation_EVENT> equations = new List<Equation_EVENT>();
            //1.以逗号,拆分内容(每一部分都是一个条件)
            string[] commas = Regex.Split(cons, SymbolConstants.SYMBOL_COMMA, RegexOptions.IgnoreCase);
            foreach (string part in commas)
            {
                Equation_EVENT equation = Cons2Eqation(part);
                if (equation != null)
                {
                    equations.Add(equation);
                }
            }

            return equations;
        }

        public static List<Equation_EVENT> Write2Equations(string writes)
        {
            //0.内容判断是否为空
            if (string.IsNullOrEmpty(writes))
            {
                return null;
            }
            List<Equation_EVENT> equations = new List<Equation_EVENT>();
            //1.以分号;拆分内容(每一部分都是一个写入信息)
            string[] semicolons = Regex.Split(writes, SymbolConstants.SYMBOL_SEMICOLON, RegexOptions.IgnoreCase);
            foreach (string part in semicolons)
            {
                Equation_EVENT equation = Cons2Eqation(part);
                if (equation != null)
                {
                    equations.Add(equation);
                }
            }

            return equations;
        }

        /// <summary>
        /// 将字符串转换为条件(单个条件)
        /// </summary>
        /// <param name="cons"></param>
        /// <returns></returns>
        public static Equation_EVENT Cons2Eqation(string cons)
        {
            //0.内容判断是否为空
            if (string.IsNullOrEmpty(cons))
            {
                return null;
            }
            //1.如果不包含"="或"<>"也直接返回
            if (!cons.Contains(SymbolConstants.SYMBOL_EQUAL) && !cons.Contains(SymbolConstants.SYMBOL_NO_EQUAL))
            {
                return null;
            }
            //2.初始化条件
            Equation_EVENT equation = new Equation_EVENT();
            string[] parts;
            if (cons.Contains(SymbolConstants.SYMBOL_EQUAL))
            {
                equation.SYMBOL = SymbolConstants.SYMBOL_EQUAL;
                parts = Regex.Split(cons, SymbolConstants.SYMBOL_EQUAL, RegexOptions.IgnoreCase);
            }
            else if (cons.Contains(SymbolConstants.SYMBOL_NO_EQUAL))
            {
                equation.SYMBOL = SymbolConstants.SYMBOL_NO_EQUAL;
                parts = Regex.Split(cons, SymbolConstants.SYMBOL_NO_EQUAL, RegexOptions.IgnoreCase);
            }
            else
            {
                return null;
            }
            //3.获取左右两侧的内容
            string leftInfo, rightInfo;
            leftInfo = parts[0].Trim();
            rightInfo = "";

            if (parts.Length > 1)
            {
                rightInfo = parts[1].Trim();
            }
            //4.处理左侧信息
            if (leftInfo.Contains(SymbolConstants.PREFIX_MEM))
            {
                //4.1.MEM_ITEM
                equation.LEFT_PREFIX = SymbolConstants.FLAG_MEM;
                equation.LEFT_MARK = leftInfo.Replace(SymbolConstants.PREFIX_MEM, "").Trim();
            }
            else
            {
                //4.2.PLC_ITEM
                equation.LEFT_PREFIX = SymbolConstants.FLAG_PLC;
                equation.LEFT_MARK = leftInfo.Replace(SymbolConstants.PREFIX_PLC, "").Trim();
            }
            //5.处理右侧信息
            if (rightInfo.Contains(SymbolConstants.PREFIX_MEM))
            {
                //5.1.MEM_ITEM
                equation.RIGHT_PREFIX = SymbolConstants.FLAG_MEM;
                equation.RIGHT_MARK = rightInfo.Replace(SymbolConstants.PREFIX_MEM, "").Trim();
            }
            else if (rightInfo.Contains(SymbolConstants.PREFIX_PLC))
            {
                //5.2.PLC_ITEM
                equation.RIGHT_PREFIX = SymbolConstants.FLAG_PLC;
                equation.RIGHT_MARK = rightInfo.Replace(SymbolConstants.PREFIX_PLC, "").Trim();
            }
            else
            {
                //5.3.SIG
                equation.RIGHT_PREFIX = SymbolConstants.FLAG_SIG;
                equation.RIGHT_MARK = rightInfo.Trim();
            }

            return equation;
        }
    }
}
